<template>

  <div class="login-container">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="login-form">
      <el-form-item label="用户名" prop="username">
        <el-input type="text" v-model="ruleForm.phone" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>

<script>
  import  qs from 'qs'
  import axios from 'axios'

  export default {
    data() {
      var checkName = (rule, value, callback) => {
        alert(value);
        if (!value) {
          return callback(new Error('名字不能为空'));
        }else{
          callback();
        }

      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          callback();
        }
      };

      return {
        ruleForm: {
          phone: '17788101690',
          password: '123456',

        },
        //验证规则
        rules: {
          password: [
            { validator: validatePass, trigger: 'blur' }
          ],

          name: [
            { validator: checkName, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        // //走验证
        // this.$refs[formName].validate((valid) => {
        //   //验证通过
        //   if (valid) {
        //     this.$axios.post("login",qs.stringify(this.ruleForm)).then( r=> {
        //       alert(r.data.code);
        //      // alert(JSON.stringify(r.data.object));
        //       sessionStorage.setItem("user",JSON.stringify(r.data.object.user));
        //       sessionStorage.getItem("user");
        //       sessionStorage.setItem("premission",JSON.stringify(r.data.object.premission))
        //
        //       this.$router.push("/main");
        //
        //
        //
        //     })
        //
        //
        //   } else {
        //     //  console.log('error submit!!');
        //     return false;
        //   }
        // });
        //
        alert(this.ruleForm.phone)
        axios.get("http://localhost:8899/backlogin/"+this.ruleForm.phone+"/"+this.ruleForm.password).then(r=>{
          alert(JSON.stringify(r.data));

          if(r.data.code==0){
            sessionStorage.setItem("token",r.data.obj.token);
            //用户id
            sessionStorage.setItem("sid",r.data.obj.sid);
            this.$router.push("/index");
          }else {
            this.$message.error("登录失败");
          }

        })

      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style scoped>

  .login-form {
    width: 350px;
    margin: 160px auto; /* 上下间距160px，左右自动居中*/
    background-color: rgb(255, 255, 255, 0.8); /* 透明背景色 */
    padding: 20px;
    border-radius: 20px; /* 圆角 */
  }

  /* 背景 */
  .login-container {
    position: absolute;
    width: 100%;
    height: 100%;
    background: url("../../assets/timg.png");
  }

  /* 标题 */
  .login-title {
    color: #303133;
    text-align: center;
  }

</style>
